"""
@File    :   register.py
@Time    :   2024/01/07 20:00:53
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   全局中间件及其注册方法
"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware


def register_middleware(app: FastAPI):
    """注册中间件

    Args:
        app (FastAPI): Fastapi实例
    """
    from src.config import get_app_settings

    # 获取配置
    settings = get_app_settings()

    # 添加CORS中间件
    app.add_middleware(
        CORSMiddleware,
        allow_origins=settings.cors_allow_origins,
        allow_methods=settings.cors_allow_methods,
        allow_headers=settings.cors_allow_headers,
        allow_credentials=settings.cors_allow_credentials,
    )
